<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="com.wlcorp.forcazamis.domain.sample.Sample" %>
<%@ page import="com.wlcorp.forcazamis.domain.sample.SubSample" %>
<%@ page import="com.wlcorp.forcazamis.domain.sample.SampleImage" %>
<%@ page import="com.wlcorp.forcazamis.sys.util.AppConstant" %>
<%@ page import="com.wlcorp.forcazamis.sys.util.CommonUtil" %>

<%
	Sample sample = (Sample)request.getAttribute("sample");
%>
<html>
<head>
	<title>样品信息更新</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<meta http-equiv="Pragma" content="no-cache"><meta http-equiv="Cache-Control" content="no-cache"><meta http-equiv="Expires" content="0">
	<link href="${pageContext.request.contextPath}/css/css.css" rel=stylesheet>
	<link rel="stylesheet" href="${pageContext.request.contextPath}/css/button_css.css" type="text/css">
	<link rel="stylesheet" href="${pageContext.request.contextPath}/css/form_css.css" type="text/css">
	<link rel="stylesheet" href="${pageContext.request.contextPath}/css/table_css.css" type="text/css">
	<link rel="stylesheet" href="${pageContext.request.contextPath}/css/pagetitle_css.css" type="text/css">
	<script language="javascript" src="${pageContext.request.contextPath}/script/validator.js"></script>
	<script language="javascript" src="${pageContext.request.contextPath}/script/calendartime.js"></script>
	<script language="javascript" src="${pageContext.request.contextPath}/script/ajax.js"></script>
	<script type="text/javascript">
		function doSave(){
		
			
			<%
				if(null == sample.getSampleId()){
			%>
			var input = document.getElementById("tmpSampleCode");
        	if("visible" == input.style.visibility){
				document.forms["sampleForm"].serialCode.value = "0";
			}
			if(document.forms["sampleForm"].tmpSampleCode.length < 1){
				if(!fieldIsNotEmpty(document.forms["sampleForm"].sampleCode)){
					return;
				}
			}
			<%}else{%>
			if(!fieldIsNotEmpty(document.forms["sampleForm"].sampleCode)){
				return;
			}
			<%}%>
			
			if(!fieldIsNotEmpty(document.forms["sampleForm"].serialCode)){
				return;
			}
			//if(!chksize(document.forms["sampleForm"].image.value)){
			//	return;
			//}
			//if(!fieldIsNotEmpty(document.forms["sampleForm"].source1)){
			//	return;
			//}
			document.forms["sampleForm"].submit();
		}
		function goback(){
			window.location = "${pageContext.request.contextPath}/sample/sampleManager.do?method=sampleManager";
		}
		//保存样品子类，包括新增和修改
		function saveSubSample(row){
			var sampleId = document.sampleForm.sampleId.value;
			if(sampleId.length < 1){
				alert("请先保存样品信息！");
				return;
			}
			var subSampleId = row.id;
			if("newRow" == subSampleId){
				subSampleId = "";
			}
			var sUrl = "${pageContext.request.contextPath}/sample/sampleManager.do?method=modifySubSample&parentSampleId=" + sampleId
				+ "&subSampleId=" + subSampleId;
			var params = "";
			var cells = row.cells;
			for(i = 0;i < cells.length;i++){
				var input = cells[i].getElementsByTagName("input");
				if(input.length > 0){
					//先验证必须输入的字段
					if("size" == input[0].name && input[0].value.length < 1){
						alert("请输入尺寸！");
						return;
					}else if("ccolorDesc" == input[0].name && input[0].value.length < 1){
						alert("请输入颜色描述（中）！");
						return;
					}
					params = params + "&" + input[0].name + "=" + input[0].value;
				}
			}
			sUrl = sUrl + params;
			ajaxRead(sUrl,function(obj){
				var result = obj.responseText;
				if("" != result){
					row.id = result;
					row.cells[row.cells.length - 1].innerHTML = '<a href="javascript:saveSubSample(document.getElementById(' + result + '))">保存</a>'
						+ '&nbsp;<a href="javascript:deleteSubSample(document.getElementById(' + result + '))">删除</a>';
					alert("保存成功！");
				}else{
					alert("保存失败！");
				}
			});
		}
		
		function addNewSubSample(){
			var table = document.getElementById("subSampleTbody");
			var rows = table.rows;
			if("newRow" == rows[rows.length - 2].id){
				alert("请先保存前一条样式子类！");
				return;
			}
			appendRow(table);
		}
		
		function appendRow(table){
          var tr = document.createElement('<tr id="newRow" bgcolor="#ffffff">');
          var td = document.createElement('<td>');
          td.innerHTML = '<input class="input" type="text" name="barCode" value="" maxlength="20"/>';
          tr.appendChild(td);
          td = document.createElement('<td>');
          td.innerHTML = '<input class="input" type="text" name="size" value="" maxlength="20"/>';
          tr.appendChild(td);
          td = document.createElement('<td>');
          td.innerHTML = '<input class="input" type="text" name="plateColor" value="" maxlength="20"/>';
          tr.appendChild(td);
          td = document.createElement('<td>');
          td.innerHTML = '<input class="input" type="text" name="ecolorDesc" value="" maxlength="20"/>';
          tr.appendChild(td);
          td = document.createElement('<td>');
          td.innerHTML = '<input class="input" type="text" name="ccolorDesc" value="" maxlength="20"/>';
          tr.appendChild(td);
          td = document.createElement('<td>');
          td.innerHTML = '<input class="input" type="text" name="memo" value="" maxlength="100"/>';
          tr.appendChild(td);
          td = document.createElement('<td>');
          td.innerHTML = '<a href="javascript:saveSubSample(document.getElementById(\'newRow\'))">保存</a>';
          tr.appendChild(td);
          table.insertBefore(tr,table.rows[table.rows.length - 1]);
        }
        
        function calSerialCode(){
        	window.showModalDialog("${pageContext.request.contextPath}/sample/sampleManager.do?method=calSerialCode",window);
        }
        
        function saveSampleImage(){
        	if(!fieldIsNotEmpty(document.forms["sampleImageForm"].imagePath)){
				return;
			}
			if(!fieldIsNotEmpty(document.forms["sampleImageForm"].serialOrder)){
				return;
			}
			//if(!chksize(document.forms["sampleImageForm"].imagePath.value)){
			//	return;
			//}
        	document.forms["sampleImageForm"].submit();
        }
        
        function deleteSampleImage(sampleImageId){
        	if(confirm("确定删除？")){
        		window.location = "${pageContext.request.contextPath}/sample/sampleManager.do?method=deleteSampleImage&id=" + sampleImageId + "&parentSampleId=${sample.sampleId}";
        	}
        }
        
        function deleteSubSample(tr){
        	if(!confirm("确定删除？")){
        		return;
        	}
        	var subSampleId = tr.id;
        	var sUrl = "${pageContext.request.contextPath}/sample/sampleManager.do?method=deleteSubSample&subSampleId=" + subSampleId
        	ajaxRead(sUrl,function(obj){
        		if("1" == obj.responseText){
        			alert("删除成功！");
        			var table = document.getElementById("subSampleTbody");
        			table.removeChild(tr);
        			
        		}else{
        			alert(obj.responseText);
        		}
        	});
        }
        
        function inputSerialCode(){
        	var input = document.getElementById("tmpSampleCode");
        	if("visible" == input.style.visibility){
        		input.style.visibility = "hidden";
        		input.style.display = "none";
        		document.forms["sampleForm"].serialCode.readOnly = true;
        	}else{
        		input.style.visibility = "visible";
        		input.style.display = "";
        		document.forms["sampleForm"].serialCode.readOnly = false;
        	}
        }
	</script>
</head>
<body>
<CENTER>
<form name="sampleForm" method="POST" action="${pageContext.request.contextPath}/sample/sampleManager.do?method=modifySample" encType="multipart/form-data">
<input type="hidden" name="sampleId" value="${sample.sampleId}"/>
<input type="hidden" name="imagePath" value="${sample.imagePath}"/>
<table width="100%" border="0" cellspacing="0" cellpadding="0" height="30">
	<tr> 
	  <td width="22" class="page_titlebar_head">&nbsp;</td>
	  <td class="page_titlebar_bg" width="981">样品编辑</td>
	</tr>
</table>

<table width="900" border="0" cellspacing="0" cellpadding="0" bgcolor="#FFFFFF" class="table_bgcolor">
      <tr> 
        <td align="right" valign="middle"> 
          <input class=button_save type=button name=save value="      保存" style="font-size:9pt" onClick="javascript:doSave()" id="btn_submit">
          <input class=button_reset name=qqq type=reset value="      恢复" style="font-size:9pt" id="btn_reset">
          <input type=button name=rst class=button_recovery value="     返回" style="font-size:9pt" onClick="javascript:history.go(-1);" id="btn_recovery">
        </td>
      </tr>
</table>

<table width="900" class="InputFrameMain" cellspacing="0" cellpadding="0" border="0" height="100">
  <tr> 
    <td colspan="6" class="table_unfold_titlebar_small_bg">&nbsp;&nbsp;<b>样品详细</b></td>
  </tr>
  <tr class="InputFrameLine">
        <td class="table_label" width="20%" id="sampleCode_TEXT">我司产品编号</td>
		<td width="30%">&nbsp;<input class="input" type="text" name="sampleCode" value="${sample.sampleCode}" style="width:150px" maxlength="20" readOnly/>
		<%
			if(null == sample.getSampleId()){
		%>
			<input name="calButton" class="" type="button" value="算号器" onclick="calSerialCode()"/>
		<%}%>
		</td>
		<td class="table_label" width="20%" id="serialCode_TEXT">数字序号</td>
		<td width="30%">&nbsp;<input class="input" type="text" name="serialCode" value="${sample.serialCode}" style="width:150px" maxlength="20" readOnly/></td>
  </tr>
  <%
	if(null == sample.getSampleId()){
  %>
  <tr id="inputCodeTR" class="InputFrameLine">
  		<td class="table_label" width="20%"><a href="javascript:inputSerialCode()"><font color="blue">手工输入编号</font></a></td>
		<td width="30%">&nbsp;<input class="input" type="text" name="tmpSampleCode" value="" style="width:150px" maxlength="20" style="visibility:hidden;display:none;"/></td>
        <td class="table_label" width="20%" id="tmpSampleCode_TEXT"></td>
		<td width="30%">&nbsp;</td>
  </tr>
  <%}%>
  <tr class="InputFrameLine">
        <td class="table_label" width="20%" id="factoryCode_TEXT">工厂编号</td>
		<td width="30%">&nbsp;<input class="input" type="text" name="factoryCode" value="${sample.factoryCode}" style="width:150px" maxlength="20"/></td>
		<td class="table_label" width="20%" id="sourceCode_TEXT">来源编号</td>
		<td width="30%">&nbsp;<input class="input" type="text" name="sourceCode" value="${sample.sourceCode}" style="width:150px" maxlength="20"/></td>
  </tr>
  <tr class="InputFrameLine">
        <td class="table_label" width="20%" id="sourceType_TEXT">来源类型</td>
		<td width="80%" colspan="3">&nbsp;<input type="radio" name="sourceType" value="<%=AppConstant.SOURCE_TYPE_DEVELOPERCS%>" checked/>研发改样&nbsp;
			<input type="radio" name="sourceType" value="<%=AppConstant.SOURCE_DEVELOPERMD%>" <%if(null != sample.getSourceType() && AppConstant.SOURCE_DEVELOPERMD == sample.getSourceType().intValue()){out.print("checked");}%>/>研发模仿&nbsp;
			<input type="radio" name="sourceType" value="<%=AppConstant.SOURCE_DEVELOPEROR%>" <%if(null != sample.getSourceType() && AppConstant.SOURCE_DEVELOPEROR == sample.getSourceType().intValue()){out.print("checked");}%>/>研发原创&nbsp;
			<input type="radio" name="sourceType" value="<%=AppConstant.SOURCE_BUYS%>" <%if(null != sample.getSourceType() && AppConstant.SOURCE_BUYS == sample.getSourceType().intValue()){out.print("checked");}%>/>直接买样&nbsp;
			<input type="radio" name="sourceType" value="<%=AppConstant.SOURCE_CUSTOMERS%>" <%if(null != sample.getSourceType() && AppConstant.SOURCE_CUSTOMERS == sample.getSourceType().intValue()){out.print("checked");}%>/>客户来样&nbsp;
			<input type="radio" name="sourceType" value="<%=AppConstant.SOURCE_FACTORYS%>" <%if(null != sample.getSourceType() && AppConstant.SOURCE_FACTORYS == sample.getSourceType().intValue()){out.print("checked");}%>/>工厂来样&nbsp;
			<input type="radio" name="sourceType" value="<%=AppConstant.SOURCE_OUTS%>" <%if(null != sample.getSourceType() && AppConstant.SOURCE_OUTS == sample.getSourceType().intValue()){out.print("checked");}%>/>出货样&nbsp;
			<input type="radio" name="sourceType" value="<%=AppConstant.SOURCE_CHANGES%>" <%if(null != sample.getSourceType() && AppConstant.SOURCE_CHANGES == sample.getSourceType().intValue()){out.print("checked");}%>/>改款&nbsp;
		</td>
  </tr>
  <tr class="InputFrameLine">
        <td class="table_label" width="20%" id="source1_TEXT">来源1</td>
		<td width="80%" colspan="3">&nbsp;<input class="input" type="text" name="source1" value="${sample.source1}" style="width:150px" maxlength="20"/>
		</td>
  </tr>
  <tr class="InputFrameLine">
        <td class="table_label" width="20%" id="source2_TEXT">来源2</td>
		<td width="80%" colspan="3">&nbsp;<input class="input" type="text" name="source2" value="${sample.source2}" style="width:150px" maxlength="20"/>
		</td>
  </tr>
  <tr class="InputFrameLine">
        <td class="table_label" width="20%" id="source3_TEXT">来源3</td>
		<td width="80%" colspan="3">&nbsp;<input class="input" type="text" name="source3" value="${sample.source3}" style="width:150px" maxlength="20"/>
		</td>
  </tr>
  <tr class="InputFrameLine">
        <td class="table_label" width="20%" id="createdBy_TEXT">添加人</td>
		<td width="30%">&nbsp;<input class="input" type="text" name="createdBy" value="${sample.createdBy.staffName}" style="width:150px" maxlength="20" disabled/></td>
		<td class="table_label" width="20%" id="modifiedDate_TEXT">最近更新日期</td>
		<td width="30%">&nbsp;<input class="input" type="text" name="modifiedDate" value="${sample.modifiedDate}" style="width:150px" maxlength="20" disabled/></td>
  </tr>
  <tr class="InputFrameLine">
        <td class="table_label" width="100%" colspan="4">
        	<table id="" width="98%" border="0" cellspacing="1" cellpadding="0" bgcolor="#999999">
        	  <tbody id="subSampleTbody">
	          <tr bgcolor="#E7E7E7">
	            <td>条形码</td>
	            <td>尺寸</td>
	            <td>电镀色号</td>
	            <td>颜色描述（英）</td>
	            <td>颜色描述（中）</td>
	            <td>说明</td>
	            <td>动作</td>
	          </tr>
	          <%
	          	if(null != sample.getSubSampleSet() && sample.getSubSampleSet().size() > 0){
	          		for(Iterator iter = sample.getSubSampleSet().iterator();iter.hasNext();){
	          			SubSample subSample = (SubSample)iter.next();
	          			if(AppConstant.RECORD_DELETED == subSample.getFlag().intValue()){
	          				continue;
	          			}
	          %>
	          <tr id="<%=subSample.getSubSampleId()%>" bgcolor="#ffffff">
	            <td><input class="input" type="text" name="barCode" value="<%=subSample.getBarCode()%>" maxlength="20"/></td>
	            <td><input class="input" type="text" name="size" value="<%=subSample.getSize()%>" maxlength="20"/></td>
	            <td><input class="input" type="text" name="plateColor" value="<%=subSample.getPlateColor()%>" maxlength="20"/></td>
	            <td><input class="input" type="text" name="ecolorDesc" value="<%=subSample.getEcolorDesc()%>" maxlength="20"/></td>
	            <td><input class="input" type="text" name="ccolorDesc" value="<%=subSample.getCcolorDesc()%>" maxlength="20"/></td>
	            <td><input class="input" type="text" name="memo" value="<%=subSample.getMemo()%>" maxlength="100"/></td>	            
	            <td><a href="javascript:saveSubSample(document.getElementById('<%=subSample.getSubSampleId()%>'))">保存</a>
	            <a href="javascript:deleteSubSample(document.getElementById('<%=subSample.getSubSampleId()%>'))">删除</a></td>
			  </tr>
			  <%}}%>
			  <tr id="newRow" bgcolor="#ffffff">
	            <td><input class="input" type="text" name="barCode" value="" maxlength="20"/></td>
	            <td><input class="input" type="text" name="size" value="" maxlength="20"/></td>
	            <td><input class="input" type="text" name="plateColor" value="" maxlength="20"/></td>
	            <td><input class="input" type="text" name="ecolorDesc" value="" maxlength="20"/></td>
	            <td><input class="input" type="text" name="ccolorDesc" value="" maxlength="20"/></td>
	            <td><input class="input" type="text" name="memo" value="" maxlength="100"/></td>	            
	            <td><a href="javascript:saveSubSample(document.getElementById('newRow'))">保存</a></td>
			  </tr>
			  <tr bgcolor="#ffffff">
	            <td colspan="7" align="center"><input type="button" name="addnewSubSample_button" value="新增样式子类" onclick="addNewSubSample()"/></td>
			  </tr>
			  </tbody>
		</table>
        </td>
  </tr>
  <tr>
  	<td class="table_label" width="100%" colspan="4">
		<table width="100%" class="InputFrameMain" cellspacing="0" cellpadding="0" border="0" height="100">
		  <tr> 
			<td width="100%" class="table_unfold_titlebar_small_bg" colspan="4">&nbsp;&nbsp;<b>样品图片</b></td>
		  </tr>
		  <tr class="InputFrameLine">
		  		<td width="20%"></td>
		        <td width="80%">
		        <%
		        if(null != sample.getImagePath()){%>
		        	<img src="${pageContext.request.contextPath}/<%=sample.getImagePath()%>"/>
		        <%}%>
		        </td>
		  </tr>
		  <tr class="InputFrameLine">
		  		<td width="20%"></td>
		        <td width="80%">订单用图<input name="image" type="file" class="input"/></td>
		  </tr>
		</table>
  	</td>
  </tr>
</table>
</form>
<form name="sampleImageForm" method="POST" action="${pageContext.request.contextPath}/sample/sampleManager.do?method=saveSampleImage" encType="multipart/form-data">
<input type="hidden" name="parentSampleId" value="<%=sample.getSampleId()%>"/>
<table width="900" class="InputFrameMain" cellspacing="0" cellpadding="0" border="0" height="100">
  <tr> 
	<td width="100%" class="table_unfold_titlebar_small_bg" colspan="4">&nbsp;&nbsp;<b>样品附属图片</b></td>
  </tr>
  <%
  	if(null != sample.getSampleImageSet() && sample.getSampleImageSet().size() > 0){
  		for(Iterator iter = sample.getSampleImageSet().iterator();iter.hasNext();){
  			SampleImage sampleImage = (SampleImage)iter.next();
  %>
  <tr class="InputFrameLine">
  		<td width="20%" align="center"><a href="javascript:deleteSampleImage(<%=sampleImage.getId()%>);">删除</a></td>
        <td width="80%" colspan="3"><img src="${pageContext.request.contextPath}/<%=sampleImage.getImagePath()%>"/></td>
  </tr>
  <tr class="InputFrameLine">
        <td class="table_label" width="20%">附图中文说明：</td>
		<td width="30%">&nbsp;<%=CommonUtil.nullToStr(sampleImage.getChineseDesc())%></td>
		<td class="table_label" width="20%">附图英文说明：</td>
		<td width="30%">&nbsp;<%=CommonUtil.nullToStr(sampleImage.getEnglishDesc())%></td>
  </tr>
  <%}}else{%>
  <tr class="InputFrameLine">
        <td width="100%" colspan="4" align="center">没有附属图片</td>
  </tr>
  <%}%>
  <tr class="InputFrameLine">
        <td class="table_label" width="20%" id="chineseDesc_TEXT">附图中文说明</td>
		<td width="30%">&nbsp;<input class="input" type="text" name="chineseDesc" value="" style="width:150px" maxlength="50"/></td>
		<td class="table_label" width="20%" id="englishDesc_TEXT">附图英文说明</td>
		<td width="30%">&nbsp;<input class="input" type="text" name="englishDesc" value="" style="width:150px" maxlength="50"/></td>
  </tr>
  <tr class="InputFrameLine">
        <td class="table_label" width="20%" id="imagePath_TEXT">图片上传</td>
		<td width="30%">&nbsp;<input class="input" type="file" name="imagePath" value="" style="width:150px" maxlength="20"/></td>
		<td class="table_label" width="20%" id="serialOrder_TEXT">附图序号</td>
		<td width="30%">&nbsp;<input class="input" type="text" name="serialOrder" value="" style="width:150px" maxlength="20"/></td>
  </tr>
  <tr class="InputFrameLine">
  	<td width="100%" colspan="4" align="center"><input type="button" class="button_common" name="saveImageButton" value="保存图片" onclick="javascript:saveSampleImage()"></td>
  </tr>
</table>
</form>
</CENTER>
${message}
</body>
</html>
